import React from 'react';
import { useAppDispatch, useAppSelector } from '../store';
import { updateByIdAction } from '../store/action/todo';

export default function Main() {
  const { list } = useAppSelector((state) => state.todo);

  // 2. 使用useAppDispatch获取dispatch 💥
  const dispatch = useAppDispatch();

  return (
    <section className="main">
      <input id="toggle-all" className="toggle-all" type="checkbox" />
      <label htmlFor="toggle-all">全选</label>
      <ul className="todo-list">
        {list.map((item) => {
          return (
            <li key={item.id} className={item.isDone ? 'completed' : ''}>
              <div className="view">
                <input
                  onChange={() => dispatch(updateByIdAction(item.id))}
                  className="toggle"
                  type="checkbox"
                  checked={item.isDone}
                />
                <label>{item.task}</label>
                <button className="destroy"></button>
              </div>
            </li>
          );
        })}
      </ul>
    </section>
  );
}
